import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


public class robo extends Enemy
{

    Color alpha = new Color(0,0,0,0);
    boolean right=false,edge=false,ramp=false,dead=false;   
    int a,velY=0,hurt=0,health=8,counter=0;  
    Zony z;

    public robo(boolean e,boolean r){
        ramp = r;
        edge = e;
        }
        
    public robo(){
        }
     
    protected void addedToWorld(World world)
    {
        setImage(new GreenfootImage(((it) getWorld()).getRobo())); 
        Zony it = (Zony) getWorld().getObjects(Zony.class).get(0);
        z=it;
    }     
        
    public void act() 
    {
        counter++;
        
        if(right)
            a=1;
        else
            a=-1;           
        setLocation(getX()+a,getY()+velY);
        if(counter%40==0 && Math.abs(z.getX()-(getX()+100*a))<100 && Math.abs(z.getY()-getY())<20)
            getWorld().addObject(new projectile(2,5*a,0,false),getX(),getY());
            
        if(wall() || ramp() || edge() || (z.getX()<getX()+100 && z.getX()>getX() && !right && Math.abs(getY()-z.getY())<20) || (z.getX()>getX()-100 && z.getX()<getX() && right && Math.abs(getY()-z.getY())<20)){
            getImage().mirrorHorizontally();
            right=!right;
        }
            
        if(((it)getWorld()).ok(getX(),getY()+14)){
            if(velY!=8)velY++;
        }else  velY=0;
        
        while(!((it)getWorld()).ok(getX()+a,getY()+13)){
            setLocation(getX(),getY()-1);
            velY=0;
        }
        if(hurt!=0)hurt--;
            
    } 
    
    
    public void hurt(int h){
        double ang,amp;
        velY=-3;
        ramp=false;
        if(hurt==0)
            health=health-h;
        hurt=3;
        if(health<1){
            for(int f=0;f<20;f++){
                 ang = 3.14159*Math.random();
                 amp = (Math.random()+3);
                 getWorld().addObject(new projectile(Greenfoot.getRandomNumber(3)+2,amp*Math.cos(ang),-amp*Math.sin(ang),true),getX(),getY());
             }
            ((it)getWorld()).removeObject(this);
        }
    }
    
    public boolean edge(){
        return ((it)getWorld()).ok(getX()+2*a,getY()+21) && edge;
    }
    
    public boolean ramp(){
        return (!((it)getWorld()).ok(getX()+2*a,getY()+13) || ((it)getWorld()).ok(getX()+2*a,getY()+14)) && ramp;
    }
    
    public boolean wall(){
        int temp = 4;
        if(ramp)temp=7;
        return !((it)getWorld()).ok(getX()+temp*a,getY()+9);
    }
    public boolean isDeadly(){
        return true;
    }
    
}